<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-card title="联系我们" :bordered="false" :labelCol="labelCol" :wrapperCol="wrapperCol" style="padding: 30px">
          <a-row>
            <a-col :span="12">
              <a-form-model-item label="电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
                <a-input v-model="model.phone" placeholder="请输入电话"  style="width: 80%" ></a-input><span style="color: red">(多个以逗号分割)</span>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol"  prop="email">
                <a-input v-model="model.address" placeholder="请输入地址"  ></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-card>
        <a-card title="关于平台" :hoverable="true" style="padding: 30px">
          <a-row>
            <a-col :span="24">
              <a-form-model-item label="平台简介" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="richText2">
                <j-editor v-model="model.richText2" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-card>
        <a-card title="服务对象" :hoverable="true" style="padding: 30px">
          <a-row>
            <a-col :span="24">
              <a-form-model-item label="服务对象详情" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="richText">
                <j-editor v-model="model.richText" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-card>
<!--        <a-card title="服务微信" :hoverable="true" style="padding: 30px">
          <a-row>
          <a-col :span="24">
            <a-form-model-item label="服务微信" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="linkcode">
              <j-image-upload is-multiple v-model="model.linkcode" placeholder="请上传二维码"  ></j-image-upload>
            </a-form-model-item>
          </a-col>
          </a-row>
        </a-card>-->
<!--        <a-card title="相关链接" :hoverable="true" style="padding: 30px">
          <a-row>
            <a-col :span="24">
              <a-form-model-item label="相关链接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qrcode">
                <j-image-upload is-multiple v-model="model.qrcode" placeholder="请上传二维码"  ></j-image-upload>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-card>-->
        <a-form-model-item style="margin-top: 100px">
          <a-button  @click="submitForm" style="margin-left: 50%"type="primary">提交</a-button>
        </a-form-model-item>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import moment from 'moment'
  import {BaiduMap, BmAutoComplete, BmLocalSearch, BmMarker,BmlCurveLine,BmCircle,BmPolyline,BmOverlay,BmLabel} from 'vue-baidu-map'
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'CompanyForm',
    components: {
      BaiduMap,
      BmLocalSearch,
      BmMarker
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      //判断是否抽屉页面
      drawer: {
        type: Boolean,
        default: false,
        required: false
      },
      //关联字段
      mainId: {
        type: String,
        default: '',
        required: false
      }
    },
    data () {
      return {
        coverImg:'',
        startTime:'00:00:00',
        endTime:'23:00:00',
        start:'',
        end:'',
        model:{
          mainId:this.mainId,
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 },
        },

        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/company/add",
          edit: "/company/edit",
          queryById: "/company/queryById",
        },
        map: {
          lng: '',
          lat: '',
          zoom: 13,
        },
        marker: {
          lng: '',
          lat: '',
        },
        center: {
          lng: '',
          lat: '',
        },
        keyword: '',
        searchList: [],
        center: { lng: 117.290574, lat: 34.212666 },
        zoom: 13,
        validatorRules: {
          address: [
            { required: true, message: '请输入地址!'},
          ],
         /* qrcode: [
            { required: true, message: '请上传二维码!'},
          ],*/
          /*linkcode: [
            { required: true, message: '请上传二维码!'},
          ],*/
          phone: [
            { required: true, message: '请输入联系方式!'},
          ],
        },
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.edit();
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit () {
        getAction(this.url.queryById).then(res=>{
          if(res.success){
            this.model = Object.assign({}, res.result);
          }else{
            this.$message.error(res.message)
          }
        })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (!valid) {
            that.$emit('error');//销毁提交按钮的loading
          }else{
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.$emit('error');
            })
          }
        })
      },
    }
  }
</script>
<style>
  .map {
    width: 100%;
    height: 300px;
  }
</style>